package travels

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"practice/internal/service"
)

type List struct {
	Page     int    `form:"page" json:"page" xml:"page"  `
	PageSize int    `form:"pageSize" json:"pageSize" xml:"pageSize" `
	Title    string `form:"title" json:"title" xml:"title"  binding:"required"`
}

func TravelList(c *gin.Context) {
	var json List
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	page := json.Page
	pageSize := json.PageSize
	if page == 0 { // 如果 page 为零值，则设置为默认值 1
		page = 1
	}
	if pageSize == 0 { // 如果 pageSize 为零值，则设置为默认值 10
		pageSize = 10
	}
	title := json.Title
	list, err := service.TravelLists(title, page, pageSize)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "展示失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  "查询成功",
		"data": list,
	})
}
